//
//  LPFWelcomeVC.swift
//  WeiBo
//
//  Created by mac on 16/4/19.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

import SDWebImage

class LPFWelcomeVC: UIViewController {
    
    override func loadView() {
        view = bgImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //加载UI
        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //执行动画
        setHeadImageViewAnim()
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        //约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 85, height: 85))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(16)
        }
        
    }
    
    // MARK: - 懒加载控件
    //背景图片
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //用户头像
    private lazy var headImageView: UIImageView = {
        let image = UIImageView(image: UIImage(named: "avatar_default_big"))
        image.layer.cornerRadius = 85 / 2
        image.layer.borderColor = ThemeColor.CGColor
        image.layer.borderWidth = 2
        image.clipsToBounds = true
        image.sd_setImageWithURL(NSURL(string: LPFUserAccountTools.sharedTools.userAccount?.avatar_large ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
        return image
    }()
    // 欢迎回来
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), textFont: 17)
        label.textAlignment = .Center
        label.text = "欢迎回来"
        label.alpha = 0
        return label
    }()
    
    // MARK: - 动画效果
    private func setHeadImageViewAnim(){
        // 更新我们的headImageView 约束
        self.headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        //阻尼动画
        UIView.animateWithDuration(2, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            // 需要刷新布局
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                //动画结束后干的事情
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    //显示label
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //发送通知 改变控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchDefaultVcNSNotification, object: "welcome")
                })
        }
    }

}
